package org.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@EnableAutoConfiguration
@RequestMapping(path = "/logistics", produces = "application/json;charset=UTF-8")
public class LogisticsAnalyseController {
    @Autowired
    private JdbcTemplate template;


    /**
     * 每个销售区域的物流商品数量、销售金额
     * @return
     * @throws IOException
     */
    @RequestMapping(path = "/districtGoodsCnt", method = RequestMethod.GET)
    public EchartData districtGoodsCnt() throws IOException {
        return template.query("select * from visual.ads_logistics_district_goods_cnt order by sales_region", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
                data.zdata.add(rs.getBigDecimal(3));
            }
            return data;
        });
    }

    /**
     * 不同货品的发货数量
     * @return
     * @throws IOException
     */
    @RequestMapping(path = "/goodsCnt", method = RequestMethod.GET)
    public EchartData goodsCnt() throws IOException {
        return template.query("SELECT * from visual.ads_logistics_goods_cnt order by quantity desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }

    /**
     * 货品用户反馈商品分布
     * @return
     * @throws IOException
     */
    @RequestMapping(path = "/goodsFeedbackCnt", method = RequestMethod.GET)
    public EchartData goodsFeedbackCnt() throws IOException {
        return template.query("select * from visual.ads_logistics_goods_feedback_cnt order by quantity desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }

    /**
     * 每个销售区域的物流交货状态情况
     * @return
     * @throws IOException
     */
    @RequestMapping(path = "/districtGoodsStatus", method = RequestMethod.GET)
    public EchartData districtGoodsStatus() throws IOException {
        return template.query("select * from visual.ads_logistics_district_goods_status order by sales_region", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
                data.zdata.add(rs.getInt(3));
            }
            return data;
        });
    }

    /**
     * 每月的销售金额
     * @return
     * @throws IOException
     */
    @RequestMapping(path = "/monthAmount", method = RequestMethod.GET)
    public EchartData monthAmount() throws IOException {
        return template.query("select * from visual.ads_logistics_month_amount order by month_key", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getBigDecimal(2));
            }
            return data;
        });
    }


    /**
     * 每月的物流商品数
     * @return
     * @throws IOException
     */
    @RequestMapping(path = "/monthCnt", method = RequestMethod.GET)
    public EchartData monthCnt() throws IOException {
        return template.query("select * from visual.ads_logistics_month_cnt order by month_key", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }

    /**
     * 不同货品的销售金额
     * @return
     * @throws IOException
     */
    @RequestMapping(path = "/goodsAmount", method = RequestMethod.GET)
    public EchartData goodsAmount() throws IOException {
        return template.query("select * from visual.ads_logistics_goods_amount order by sales_amount desc", rs -> {
            EchartData data = new EchartData();
            while (rs.next()) {
                data.xdata.add(rs.getString(1));
                data.ydata.add(rs.getInt(2));
            }
            return data;
        });
    }





    public static class EchartData {

        public List<Object> xdata;
        public List<Object> ydata;
        public List<Object> zdata;
        public List<Object> wdata;

        public EchartData() {
            this.xdata = new ArrayList<Object>();
            this.ydata = new ArrayList<Object>();
            this.zdata = new ArrayList<Object>();
            this.wdata = new ArrayList<Object>();
        }


    }
}
